package com.hangzhou.xyj.engine.impl;

import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.engine.AppUpdateEngine;
import com.hangzhou.xyj.manager.ScreenManager;
import com.hangzhou.xyj.model.AppUpdateModel;
import com.hangzhou.xyj.net.handler.AppUpdateHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.AppUpdateReq;
import com.hangzhou.xyj.ui.dialog.TipsDialog;
import com.hangzhou.xyj.utils.DownloadUtil;
import com.hangzhou.xyj.utils.LogUtil;
import com.hangzhou.xyj.utils.PublicUtil;

import java.util.Date;

/**
 * Created by neo.duan on 2016/6/3.
 * 检查更新逻辑实现类
 */
public class AppUpdateEngineImpl implements AppUpdateEngine {
    private static final String TAG = "AppUpdateEngineImpl";

    /**
     * 检查更新规则：上次检查更新的时间大于一天
     *
     * @return
     */
    @Override
    public boolean isNeedUpdate() {
        //上一次检查更新版本时间
        Date lastAppCheckUpdate = CommonPreferences.getLastAppCheckUpdate(XYJApplication.mAppContext);
        Date currentDate = new Date();
        //一天之内不检查更新了
        if (currentDate.getTime() - lastAppCheckUpdate.getTime() < 24 * 60 * 60 * 1000) {
            return false;
        }
        //更新上次检查更新时间
        CommonPreferences.saveLastAppCheckUpdate(XYJApplication.mAppContext, currentDate.getTime());
        return true;
    }

    @Override
    public boolean isShowUpdateDialog(AppUpdateModel model) {
        if (model == null) {
            return false;
        }
        int currentAppVersion = PublicUtil.getCurrentAppVersion(XYJApplication.getInstance());
        return model.getVersionCode() > currentAppVersion;
    }

    @Override
    public void downLoadApk(String appUrl) {
        DownloadUtil.downloadApk(XYJApplication.mAppContext, appUrl);
    }

    @Override
    public void showUpdateDialog(final AppUpdateModel model) {
            TipsDialog dialog = new TipsDialog(ScreenManager.getInstance().currentActivity(), "提示", model.getUpdateText(), "立即更新", "以后再说") {
                @Override
                protected void OnClickNegativeButton() {
                    this.dismiss();
                }

                @Override
                protected boolean OnClickPositiveButton() {
                    downLoadApk(model.getAppUrl());
                    this.dismiss();
                    return true;
                }
            };
            dialog.show();
    }

    /**
     * app检查更新
     */
    public void checkAppUpdate() {
        //根据规则如果不需要检查更新了，则不发送请求
        if (!isNeedUpdate()) {
            return;
        }
        AppUpdateReq req = new AppUpdateReq();
        HttpLoader.getInstance().sendRequest(new AppUpdateHandler(req, new HttpResponseListener() {
            @Override
            public void onStart() {
                LogUtil.d(TAG,"checkAppUpdate onStart()");
            }

            @Override
            public void onSuccess(Object model) {
                //获取到版本更新配置文件
                AppUpdateModel updateModel = (AppUpdateModel) model;
                if (isShowUpdateDialog(updateModel)) {
                    //弹出版本更新对话框
                    showUpdateDialog(updateModel);
                }
            }

            @Override
            public void onFail(String msg) {
                LogUtil.d(TAG,"checkAppUpdate onFail()");
            }

            @Override
            public void onDone(String msg) {
                LogUtil.d(TAG,"checkAppUpdate onDone()");
            }

            @Override
            public void onError() {
                LogUtil.d(TAG,"checkAppUpdate onError()");
            }
        },0));
    }
}
